package com.luke.star.persist.mapper;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.DeleteProvider;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.UpdateProvider;
import org.apache.ibatis.type.JdbcType;

import com.luke.star.bean.StTaskNoteVo;
import com.luke.star.persist.po.StTaskNote;
import com.luke.star.persist.po.StTaskNoteExample;

public interface StTaskNoteMapper {
    @SelectProvider(type=StTaskNoteSqlProvider.class, method="countByExample")
    long countByExample(StTaskNoteExample example);

    @DeleteProvider(type=StTaskNoteSqlProvider.class, method="deleteByExample")
    int deleteByExample(StTaskNoteExample example);

    @Delete({
        "delete from st_task_note",
        "where task_id = #{taskId,jdbcType=VARCHAR}"
    })
    int deleteByPrimaryKey(String taskId);

    @Insert({
        "insert into st_task_note (task_id, add_star_num, ",
        "now_star_num, note, ",
        "create_time)",
        "values (#{taskId,jdbcType=VARCHAR}, #{addStarNum,jdbcType=INTEGER}, ",
        "#{nowStarNum,jdbcType=INTEGER}, #{note,jdbcType=VARCHAR}, ",
        "#{createTime,jdbcType=TIMESTAMP})"
    })
    int insert(StTaskNote record);

    @InsertProvider(type=StTaskNoteSqlProvider.class, method="insertSelective")
    int insertSelective(StTaskNote record);

    @SelectProvider(type=StTaskNoteSqlProvider.class, method="selectByExample")
    @Results({
        @Result(column="task_id", property="taskId", jdbcType=JdbcType.VARCHAR, id=true),
        @Result(column="add_star_num", property="addStarNum", jdbcType=JdbcType.INTEGER),
        @Result(column="now_star_num", property="nowStarNum", jdbcType=JdbcType.INTEGER),
        @Result(column="note", property="note", jdbcType=JdbcType.VARCHAR),
        @Result(column="create_time", property="createTime", jdbcType=JdbcType.TIMESTAMP)
    })
    List<StTaskNote> selectByExample(StTaskNoteExample example);

    @Select({
        "select",
        "task_id, add_star_num, now_star_num, note, create_time",
        "from st_task_note",
        "where task_id = #{taskId,jdbcType=VARCHAR}"
    })
    @Results({
        @Result(column="task_id", property="taskId", jdbcType=JdbcType.VARCHAR, id=true),
        @Result(column="add_star_num", property="addStarNum", jdbcType=JdbcType.INTEGER),
        @Result(column="now_star_num", property="nowStarNum", jdbcType=JdbcType.INTEGER),
        @Result(column="note", property="note", jdbcType=JdbcType.VARCHAR),
        @Result(column="create_time", property="createTime", jdbcType=JdbcType.TIMESTAMP)
    })
    StTaskNote selectByPrimaryKey(String taskId);

    @UpdateProvider(type=StTaskNoteSqlProvider.class, method="updateByExampleSelective")
    int updateByExampleSelective(@Param("record") StTaskNote record, @Param("example") StTaskNoteExample example);

    @UpdateProvider(type=StTaskNoteSqlProvider.class, method="updateByExample")
    int updateByExample(@Param("record") StTaskNote record, @Param("example") StTaskNoteExample example);

    @UpdateProvider(type=StTaskNoteSqlProvider.class, method="updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(StTaskNote record);

    @Update({
        "update st_task_note",
        "set add_star_num = #{addStarNum,jdbcType=INTEGER},",
          "now_star_num = #{nowStarNum,jdbcType=INTEGER},",
          "note = #{note,jdbcType=VARCHAR},",
          "create_time = #{createTime,jdbcType=TIMESTAMP}",
        "where task_id = #{taskId,jdbcType=VARCHAR}"
    })
    int updateByPrimaryKey(StTaskNote record);
    
    //--------------------------------------by human
    
    @Select({
    	"select n.*,t.task_name ",
    	" from St_Task_note n join St_Task t on t.id=n.task_id",
    	"where",
    	" ('${taskName}'='' OR t.task_name like '%${taskName}%')",
    	" and ('${createTimeBegin}'='' OR n.create_time >= str_to_date('${createTimeBegin}','%Y-%m-%d') )",
    	" and ('${createTimeEnd}'='' OR n.create_time <= str_to_date('${createTimeEnd}','%Y-%m-%d') )",
    	" order by create_time desc ",
    	" limit ${begin},${pageSize}",
    })
    @Results({
        @Result(column="task_id", property="taskId", jdbcType=JdbcType.VARCHAR),
        @Result(column="add_star_num", property="addStarNum", jdbcType=JdbcType.INTEGER),
        @Result(column="now_star_num", property="nowStarNum", jdbcType=JdbcType.INTEGER),
        @Result(column="note", property="note", jdbcType=JdbcType.VARCHAR),
        @Result(column="create_time", property="createTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="task_name", property="taskName", jdbcType=JdbcType.VARCHAR),
    })
    List<StTaskNote> listTaskLogByPage(Map<String,Object> map);
    
	@Select({
		"select count(1) ",
    	" from St_Task_note n join St_Task t on t.id=n.task_id",
    	"where",
    	" ('${taskName}'='' OR t.task_name like '%${taskName}%')",
    	" and ('${createTimeBegin}'='' OR n.create_time >= str_to_date('${createTimeBegin}','%Y-%m-%d') )",
    	" and ('${createTimeEnd}'='' OR n.create_time <= str_to_date('${createTimeEnd}','%Y-%m-%d') )",
	})
	int countTaskLogByPage(Map<String,Object> map);       
    
	
	@Select({
		"select t.*,k.task_name,k.weight,k.task_type from ",
		"	(select tmp.day_str,tmp.task_id,sum(tmp.add_star_num) sum_star from ",
		"		(select s.*,DATE_FORMAT(s.create_time,'%Y%m%d') as day_str from st_task_note s",
		"         where create_time BETWEEN STR_TO_DATE('${beginDayStr}','%Y%m%d') and STR_TO_DATE('${endDayStr}','%Y%m%d')) tmp",
		"	group by tmp.day_str,tmp.task_id) t",
		"left join st_task k on k.id=t.task_id order by t.day_str",
	})
	@Results({
		@Result(column="task_id", property="taskId", jdbcType=JdbcType.VARCHAR),
		@Result(column="sum_star", property="sumStar", jdbcType=JdbcType.INTEGER),
		@Result(column="day_str", property="dayStr", jdbcType=JdbcType.VARCHAR),
		@Result(column="task_name", property="taskName", jdbcType=JdbcType.VARCHAR),
		@Result(column="weight", property="weight", jdbcType=JdbcType.INTEGER),
		@Result(column="task_type", property="taskType", jdbcType=JdbcType.VARCHAR),
	})
	List<StTaskNoteVo> listTaskLogByDay(@Param("beginDayStr") String beginDayStr,@Param("endDayStr") String endDayStr);
	
	
	@Select({
		"select t.*,k.task_name,k.weight,k.task_type from ",
		"	(select tmp.month_str,tmp.task_id,sum(tmp.add_star_num) sum_star from ",
		"		(select s.*,DATE_FORMAT(s.create_time,'%Y%m') as month_str from st_task_note s",
		"         where create_time BETWEEN STR_TO_DATE('${beginDayStr}','%Y%m%d') and STR_TO_DATE('${endDayStr}','%Y%m%d')) tmp",
		"	group by tmp.month_str,tmp.task_id ) t",
		"left join st_task k on k.id=t.task_id order by t.month_str",
		"order by t.month_str",
	})
	@Results({
		@Result(column="task_id", property="taskId", jdbcType=JdbcType.VARCHAR),
		@Result(column="sum_star", property="sumStar", jdbcType=JdbcType.INTEGER),
		@Result(column="month_str", property="monthStr", jdbcType=JdbcType.VARCHAR),
		@Result(column="task_name", property="taskName", jdbcType=JdbcType.VARCHAR),
		@Result(column="weight", property="weight", jdbcType=JdbcType.INTEGER),
		@Result(column="task_type", property="taskType", jdbcType=JdbcType.VARCHAR),
	})
	List<StTaskNoteVo> listTaskLogByMonth(@Param("beginDayStr") String beginDayStr,@Param("endDayStr") String endDayStr);
    
    
}